/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * chatServer.java
 *
 * Created on Oct 5, 2009, 12:36:06 AM
 */
package cititool.chat.server.UI;

import cititool.chat.model.SystemConstants;
import cititool.chat.server.handler.MainServer;
import cititool.chat.server.*;
import cititool.global.AppContext;
import cititool.util.ComponentHelper;
import cititool.util.StringHelper;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Administrator
 */
public class ChatServerFrame extends javax.swing.JFrame {

    /** Creates new form chatServer */
    public ChatServerFrame() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        initdata();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        clientTab = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        clientTree = new javax.swing.JTree();
        jTabbedPane3 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        ConsolePane = new javax.swing.JTabbedPane();
        serverPane = new javax.swing.JPanel();
        addServerBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        serverTab = new javax.swing.JTable();
        deleteServerBtn = new javax.swing.JButton();
        startServerBtn = new javax.swing.JButton();
        modifyServerBtn = new javax.swing.JButton();
        stopServerBtn = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        servername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        port = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        description = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        createdatetime = new javax.swing.JTextField();
        lastmodifydatetime = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        restartServerBtn = new javax.swing.JButton();
        serverControlPane = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        serverlog = new javax.swing.JTextPane();
        DBPane = new javax.swing.JPanel();
        startdbBtn = new javax.swing.JButton();
        dbfolder = new javax.swing.JTextField();
        dbfolderBtn = new javax.swing.JButton();
        jScrollPane7 = new javax.swing.JScrollPane();
        dblog = new javax.swing.JTextPane();
        picfolder = new javax.swing.JTextField();
        picfolderBtn = new javax.swing.JButton();
        testPane = new javax.swing.JPanel();
        test = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea3 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cititool.MainApp.class).getContext().getResourceMap(ChatServerFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(350);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setAutoscrolls(true);
        jSplitPane1.setName("jSplitPane1"); // NOI18N
        jSplitPane1.setOneTouchExpandable(true);

        clientTab.setName("clientinfo"); // NOI18N
        clientTab.setPreferredSize(new java.awt.Dimension(300, 350));

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane6.setName("jScrollPane6"); // NOI18N

        clientTree.setModel(null);
        clientTree.setAutoscrolls(true);
        clientTree.setName("clientTree"); // NOI18N
        jScrollPane6.setViewportView(clientTree);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        clientTab.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jSplitPane1.setTopComponent(clientTab);

        jTabbedPane3.setName("jTabbedPane3"); // NOI18N
        jTabbedPane3.setPreferredSize(new java.awt.Dimension(300, 150));

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 295, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 172, Short.MAX_VALUE)
        );

        jTabbedPane3.addTab(resourceMap.getString("jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jSplitPane1.setRightComponent(jTabbedPane3);

        ConsolePane.setName("ConsolePane"); // NOI18N

        serverPane.setName("serverPane"); // NOI18N

        addServerBtn.setText(resourceMap.getString("addServerBtn.text")); // NOI18N
        addServerBtn.setName("addServerBtn"); // NOI18N
        addServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addServerBtnActionPerformed(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        serverTab.setFont(resourceMap.getFont("serverTab.font")); // NOI18N
        serverTab.setForeground(resourceMap.getColor("serverTab.foreground")); // NOI18N
        serverTab.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ServerName", "port", "status", "clientCount"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        serverTab.setName("serverTab"); // NOI18N
        serverTab.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                serverTabMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(serverTab);
        serverTab.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("serverTab.columnModel.title0")); // NOI18N
        serverTab.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("serverTab.columnModel.title1")); // NOI18N
        serverTab.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("serverTab.columnModel.title2")); // NOI18N
        serverTab.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("serverTab.columnModel.title3")); // NOI18N

        deleteServerBtn.setText(resourceMap.getString("deleteServerBtn.text")); // NOI18N
        deleteServerBtn.setName("deleteServerBtn"); // NOI18N
        deleteServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteServerBtnActionPerformed(evt);
            }
        });

        startServerBtn.setText(resourceMap.getString("startServerBtn.text")); // NOI18N
        startServerBtn.setName("startServerBtn"); // NOI18N
        startServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startServerBtnActionPerformed(evt);
            }
        });

        modifyServerBtn.setText(resourceMap.getString("modifyServerBtn.text")); // NOI18N
        modifyServerBtn.setName("modifyServerBtn"); // NOI18N
        modifyServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyServerBtnActionPerformed(evt);
            }
        });

        stopServerBtn.setText(resourceMap.getString("stopServerBtn.text")); // NOI18N
        stopServerBtn.setName("stopServerBtn"); // NOI18N
        stopServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopServerBtnActionPerformed(evt);
            }
        });

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel6.border.title"))); // NOI18N
        jPanel6.setName("jPanel6"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        servername.setText(resourceMap.getString("servername.text")); // NOI18N
        servername.setName("servername"); // NOI18N
        servername.setNextFocusableComponent(port);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        port.setText(resourceMap.getString("port.text")); // NOI18N
        port.setName("port"); // NOI18N
        port.setNextFocusableComponent(description);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        description.setColumns(20);
        description.setRows(5);
        description.setName("description"); // NOI18N
        jScrollPane5.setViewportView(description);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        createdatetime.setEditable(false);
        createdatetime.setText(resourceMap.getString("createdatetime.text")); // NOI18N
        createdatetime.setName("createdatetime"); // NOI18N

        lastmodifydatetime.setEditable(false);
        lastmodifydatetime.setText(resourceMap.getString("lastmodifydatetime.text")); // NOI18N
        lastmodifydatetime.setName("lastmodifydatetime"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(servername, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 85, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lastmodifydatetime)
                            .addComponent(createdatetime, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(servername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(createdatetime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(lastmodifydatetime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(28, 28, 28)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        restartServerBtn.setText(resourceMap.getString("restartServerBtn.text")); // NOI18N
        restartServerBtn.setName("restartServerBtn"); // NOI18N
        restartServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restartServerBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout serverPaneLayout = new javax.swing.GroupLayout(serverPane);
        serverPane.setLayout(serverPaneLayout);
        serverPaneLayout.setHorizontalGroup(
            serverPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(serverPaneLayout.createSequentialGroup()
                .addGap(193, 193, 193)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(226, Short.MAX_VALUE))
            .addGroup(serverPaneLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(startServerBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stopServerBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(restartServerBtn)
                .addGap(83, 83, 83)
                .addComponent(addServerBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(modifyServerBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteServerBtn)
                .addContainerGap(15, Short.MAX_VALUE))
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        serverPaneLayout.setVerticalGroup(
            serverPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stopServerBtn)
                    .addComponent(startServerBtn)
                    .addComponent(deleteServerBtn)
                    .addComponent(modifyServerBtn)
                    .addComponent(addServerBtn)
                    .addComponent(restartServerBtn))
                .addGap(18, 18, 18)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        ConsolePane.addTab(resourceMap.getString("serverPane.TabConstraints.tabTitle"), serverPane); // NOI18N

        serverControlPane.setName("serverControlPane"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        serverlog.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("serverlog.border.title"))); // NOI18N
        serverlog.setContentType(resourceMap.getString("serverlog.contentType")); // NOI18N
        serverlog.setForeground(resourceMap.getColor("serverlog.foreground")); // NOI18N
        serverlog.setName("serverlog"); // NOI18N
        jScrollPane1.setViewportView(serverlog);

        javax.swing.GroupLayout serverControlPaneLayout = new javax.swing.GroupLayout(serverControlPane);
        serverControlPane.setLayout(serverControlPaneLayout);
        serverControlPaneLayout.setHorizontalGroup(
            serverControlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
        );
        serverControlPaneLayout.setVerticalGroup(
            serverControlPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverControlPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(262, Short.MAX_VALUE))
        );

        ConsolePane.addTab(resourceMap.getString("serverControlPane.TabConstraints.tabTitle"), serverControlPane); // NOI18N

        DBPane.setName("DBPane"); // NOI18N

        startdbBtn.setText(resourceMap.getString("startdbBtn.text")); // NOI18N
        startdbBtn.setName("startdbBtn"); // NOI18N
        startdbBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startdbBtnActionPerformed(evt);
            }
        });

        dbfolder.setText(resourceMap.getString("dbfolder.text")); // NOI18N
        dbfolder.setName("dbfolder"); // NOI18N

        dbfolderBtn.setText(resourceMap.getString("dbfolderBtn.text")); // NOI18N
        dbfolderBtn.setName("dbfolderBtn"); // NOI18N
        dbfolderBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbfolderBtnActionPerformed(evt);
            }
        });

        jScrollPane7.setName("jScrollPane7"); // NOI18N

        dblog.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("dblog.border.title"))); // NOI18N
        dblog.setEditable(false);
        dblog.setForeground(resourceMap.getColor("dblog.foreground")); // NOI18N
        dblog.setName("dblog"); // NOI18N
        dblog.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dblogMouseClicked(evt);
            }
        });
        jScrollPane7.setViewportView(dblog);

        picfolder.setText(resourceMap.getString("picfolder.text")); // NOI18N
        picfolder.setName("picfolder"); // NOI18N

        picfolderBtn.setText(resourceMap.getString("picfolderBtn.text")); // NOI18N
        picfolderBtn.setName("picfolderBtn"); // NOI18N
        picfolderBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                picfolderBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout DBPaneLayout = new javax.swing.GroupLayout(DBPane);
        DBPane.setLayout(DBPaneLayout);
        DBPaneLayout.setHorizontalGroup(
            DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DBPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(startdbBtn)
                    .addGroup(DBPaneLayout.createSequentialGroup()
                        .addGroup(DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(picfolder, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dbfolder, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dbfolderBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(picfolderBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(79, Short.MAX_VALUE))
            .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
        );
        DBPaneLayout.setVerticalGroup(
            DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DBPaneLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(startdbBtn)
                .addGap(18, 18, 18)
                .addGroup(DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dbfolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dbfolderBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(picfolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(picfolderBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        ConsolePane.addTab(resourceMap.getString("DBPane.TabConstraints.tabTitle"), DBPane); // NOI18N

        testPane.setName("testPane"); // NOI18N

        test.setText(resourceMap.getString("test.text")); // NOI18N
        test.setName("test"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel5.setName("jPanel5"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setName("querySql"); // NOI18N
        jScrollPane3.setViewportView(jTextArea2);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("query", jPanel5);

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTextArea3.setColumns(20);
        jTextArea3.setEditable(false);
        jTextArea3.setRows(5);
        jTextArea3.setName("queryLog"); // NOI18N
        jScrollPane4.setViewportView(jTextArea3);

        javax.swing.GroupLayout testPaneLayout = new javax.swing.GroupLayout(testPane);
        testPane.setLayout(testPaneLayout);
        testPaneLayout.setHorizontalGroup(
            testPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(testPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(testPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                    .addComponent(test)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE))
                .addContainerGap())
        );
        testPaneLayout.setVerticalGroup(
            testPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(testPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(test)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(192, Short.MAX_VALUE))
        );

        ConsolePane.addTab("test", testPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ConsolePane, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ConsolePane, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void restartServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restartServerBtnActionPerformed
        // TODO add your handling code here:
        List<MainServer> list = getSelectedServer();
        ServerContext.getServerFactory().stopServer(list);
        ServerContext.getServerFactory().startServer(list);
    }//GEN-LAST:event_restartServerBtnActionPerformed

    private void stopServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopServerBtnActionPerformed
        // TODO add your handling code here:
        List<MainServer> list = getSelectedServer();
        ServerContext.getServerFactory().stopServer(list);
}//GEN-LAST:event_stopServerBtnActionPerformed

    private void modifyServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyServerBtnActionPerformed
        // TODO add your handling code here:
        MainServer server = getFirstSelectedServer();
        if (server == null) {
            return;
        }
        ServerContext.getServerFactory().updateServer(servername.getText(), server.getServerName(), StringHelper.String2Int(port.getText(), 8888), description.getText());
        updateServerXMLConfig();
}//GEN-LAST:event_modifyServerBtnActionPerformed

    private void startServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startServerBtnActionPerformed
        // TODO add your handling code here:
        List<MainServer> list = getSelectedServer();
        ServerContext.getServerFactory().startServer(list);
}//GEN-LAST:event_startServerBtnActionPerformed

    private void deleteServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteServerBtnActionPerformed
        // TODO add your handling code here:
        List<MainServer> list = getSelectedServer();
        if (list.size() == 0) {
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "are you sure to delele all selected servers?") == JOptionPane.YES_OPTION) {
            ServerContext.getServerFactory().delServer(list);
            updateServerXMLConfig();
        }
}//GEN-LAST:event_deleteServerBtnActionPerformed

    private void serverTabMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_serverTabMouseClicked
        // TODO add your handling code here:
        if (evt.getButton() == MouseEvent.BUTTON1) {
            int index = serverTab.rowAtPoint(evt.getPoint());                       
            /**
             * edit server
             */
            if (index >=0 && index<=serverTab.getRowCount()-1) {
                int c=evt.getClickCount();
                if(c==1){
                String serverName = (String) serverTab.getValueAt(index, 0);
                MainServer server = ServerContext.getServerFactory().getServerByName(serverName);
                if (server != null) {
                    if (server.getStatus() == SystemConstants.Status.PREPARED || server.getStatus() == SystemConstants.Status.STOPED) {
                        startServerBtn.setEnabled(true);
                        stopServerBtn.setEnabled(false);
                        restartServerBtn.setEnabled(false);
                    } else if (server.getStatus() == SystemConstants.Status.STARTED) {
                        startServerBtn.setEnabled(false);
                        stopServerBtn.setEnabled(true);
                        restartServerBtn.setEnabled(true);
                    } else if (server.getStatus() == SystemConstants.Status.STOPING) {
                        startServerBtn.setEnabled(false);
                        stopServerBtn.setEnabled(false);
                        restartServerBtn.setEnabled(false);
                    }
                }

                servername.setText(server.getServerName());
                port.setText(server.getPort() + "");
                description.setText(server.getDescription());
                createdatetime.setText(server.getCreatetime());
                lastmodifydatetime.setText(server.getLastmodifytime());
                }
                if(c==2){
                    MainServer server = getFirstSelectedServer();
                    ServerContext.getServerFactory().shiftServer(server);
                }
            }
        }
}//GEN-LAST:event_serverTabMouseClicked

    private void addServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addServerBtnActionPerformed
        // TODO add your handling code here:
        List arr = new ArrayList();
        arr.add(servername);
        arr.add(port);
        if (ComponentHelper.checkArrayNull(this, arr)) {
            return;
        } else {
            MainServer server = ServerContext.getServerFactory().addServer(servername.getText(), StringHelper.String2Int(port.getText()), description.getText());
            server.setView(serverTab);
            addTableModel(server);
            updateServerXMLConfig();
            servername.setText("");
            port.setText("");
            description.setText("");
            createdatetime.setText("");
            lastmodifydatetime.setText("");

        }
}//GEN-LAST:event_addServerBtnActionPerformed

    private void startdbBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startdbBtnActionPerformed
        // TODO add your handling code here:
        Thread t = new Thread(
                new Runnable() {
                    public void run() {
                        ServerContext.startDB();
                    }
                });
        t.setDaemon(true);
        t.start();
    }//GEN-LAST:event_startdbBtnActionPerformed

    private void dbfolderBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbfolderBtnActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser=new JFileChooser(dbfolder.getText());
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setMultiSelectionEnabled(false);

        if(chooser.showDialog(this, "choose the database ")!=JFileChooser.APPROVE_OPTION){
            return ;
        }
        File f=chooser.getSelectedFile();
        dbfolder.setText(f.getPath());
        pref.put("dbpath", dbfolder.getText()+File.separator);

       ServerContext.getDB().setDbpath(dbfolder.getText()+File.separator);
    }//GEN-LAST:event_dbfolderBtnActionPerformed

    private void dblogMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dblogMouseClicked
        // TODO add your handling code here:
        ComponentHelper.clearPopup(evt, dblog);
    }//GEN-LAST:event_dblogMouseClicked

    private void picfolderBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_picfolderBtnActionPerformed
        // TODO add your handling code here:
         JFileChooser chooser=new JFileChooser(picfolder.getText());
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setMultiSelectionEnabled(false);

        if(chooser.showDialog(this, "choose the picfolder ")!=JFileChooser.APPROVE_OPTION){
            return ;
        }
        File f=chooser.getSelectedFile();
        picfolder.setText(f.getPath());
        pref.put("picpath", picfolder.getText()+File.separator);
        ServerContext.setSystemFileFolder(picfolder.getText()+File.separator);
    }//GEN-LAST:event_picfolderBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
              final ChatServerFrame dialog = new ChatServerFrame();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        dialog.dispose();
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private Vector getFirstSelectedRowData() {

        ListSelectionModel model = serverTab.getSelectionModel();
        for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++) {
            if (model.isSelectedIndex(i)) {
                Vector row = (Vector) ((DefaultTableModel) serverTab.getModel()).getDataVector().get(i);
                return row;
            }
        }
        return new Vector();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane ConsolePane;
    private javax.swing.JPanel DBPane;
    private javax.swing.JButton addServerBtn;
    private javax.swing.JTabbedPane clientTab;
    private javax.swing.JTree clientTree;
    private javax.swing.JTextField createdatetime;
    private javax.swing.JTextField dbfolder;
    private javax.swing.JButton dbfolderBtn;
    private javax.swing.JTextPane dblog;
    private javax.swing.JButton deleteServerBtn;
    private javax.swing.JTextArea description;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextArea3;
    private javax.swing.JTextField lastmodifydatetime;
    private javax.swing.JButton modifyServerBtn;
    private javax.swing.JTextField picfolder;
    private javax.swing.JButton picfolderBtn;
    private javax.swing.JTextField port;
    private javax.swing.JButton restartServerBtn;
    private javax.swing.JPanel serverControlPane;
    private javax.swing.JPanel serverPane;
    private javax.swing.JTable serverTab;
    private javax.swing.JTextPane serverlog;
    private javax.swing.JTextField servername;
    private javax.swing.JButton startServerBtn;
    private javax.swing.JButton startdbBtn;
    private javax.swing.JButton stopServerBtn;
    private javax.swing.JButton test;
    private javax.swing.JPanel testPane;
    // End of variables declaration//GEN-END:variables


    private Preferences pref;
    /**
     * add server info into JTableModel
     * @param server
     */
    private void addTableModel(MainServer server) {

        DefaultTableModel model = (DefaultTableModel) serverTab.getModel();
        Object data[] = {server.getServerName(), server.getPort(), SystemConstants.Status.toString(server.getStatus()), 0, null};
        model.addRow(data);
    }



    /**
     * load the config file server.xml information into serverTables
     */
    private void ServerXMLLoad() {
        File f = new File(AppContext.getServerConfigPath());
        if (f.exists()) {
            //read config document
            ServerContext.getServerFactory().loadServerXML(f);
            List<MainServer> list = ServerContext.getServerFactory().getServers();
            for (MainServer server : list) {
                server.setView(serverTab);
                addTableModel(server);
            }
        } else {
            try {
                f.createNewFile();
                Document doc = new Document(new Element("servers"));
                new XMLOutputter().output(doc, new FileOutputStream(f));
            } catch (IOException ex) {
                Logger.getLogger(ChatServerFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * refresh UI model into the config server.xml
     */
    private void updateServerXMLConfig() {

        File f = new File(AppContext.getServerConfigPath());
        SAXBuilder sb = new SAXBuilder();
        try {
            Document doc = sb.build(f);
            Element root = doc.getRootElement();
            root.removeContent();
            List<MainServer> servers = ServerContext.getServerFactory().getServers();
            for (MainServer s : servers) {
                String serverName = s.getServerName();
                String port = s.getPort().toString();
                String desc = s.getDescription();
                String cdt = s.getCreatetime();
                String lmdt = s.getLastmodifytime();
                Element server = new Element("server");
                Attribute create = new Attribute("createdatetime", cdt);
                Attribute lastmodify = new Attribute("lastmodifydatetime", lmdt);
                server.setAttribute(create);
                server.setAttribute(lastmodify);
                Element sn = new Element("servername");
                sn.setText(serverName);
                Element p = new Element("port");
                p.setText(port);
                Element des = new Element("description");
                des.setText(desc);
                server.addContent(sn);
                server.addContent(p);
                server.addContent(des);

                root.addContent(server);
            }
            new XMLOutputter().output(doc, new FileOutputStream(f));

        } catch (JDOMException ex) {
            Logger.getLogger(ChatServerFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ChatServerFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * initialize the data after the components  load over
     */
    private void initdata() {
        //initialize the server factory
        Thread t = new Thread(new Runnable() {
            public void run() {
                pref= Preferences.userRoot().node("/com/cititoolkit");
                String picstore=pref.get("picpath", "C:/brucexx/picstore/");
                String dbstore=pref.get("dbpath", "C:/brucexx/xmldb/");
                dbfolder.setText(dbstore);
                picfolder.setText(picstore);
                ServerContext.setServerLogger(serverlog);
                ServerContext.setDBLogger(dblog);
 
                ServerContext.instance();
                ServerContext.setSystemFileFolder(picstore);
                ServerContext.getDB().setDbpath(dbstore);
                //make system folder
                File f = new File(AppContext.getSystemFolderPath());
                if (!f.exists()) {
                    f.mkdirs();
                }
                //initialize server coonfig information
                ServerXMLLoad();
            }
        });
        t.start();
    }

    private List<MainServer> getSelectedServer() {
        List<MainServer> re = new Vector<MainServer>();
        ListSelectionModel list = serverTab.getSelectionModel();
        if (list.getMinSelectionIndex() < 0) {
            JOptionPane.showMessageDialog(this, "haven't choice any items yet");
            return re;
        }
        for (int i = list.getMinSelectionIndex(); i <= list.getMaxSelectionIndex(); i++) {
            String serverName = serverTab.getValueAt(i, 0).toString();
            MainServer server = ServerContext.getServerFactory().getServerByName(serverName);
            re.add(server);

        }
        return re;
    }

    private MainServer getFirstSelectedServer() {

        ListSelectionModel list = serverTab.getSelectionModel();
        if (list.getMinSelectionIndex() < 0) {
            JOptionPane.showMessageDialog(this, "haven't choice any items yet");
            return null;
        }
        String serverName = serverTab.getValueAt(list.getMinSelectionIndex(), 0).toString();
        MainServer server = ServerContext.getServerFactory().getServerByName(serverName);
        return server;


    }
}
